package com._51cto.healthy.service;

import com._51cto.healthy.base.BaseMapper;
import com._51cto.healthy.content.P;
import com._51cto.healthy.mapper.OrderMapper;
import com._51cto.healthy.mapper.OrderSettingMapper;
import com._51cto.healthy.pojo.Order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@DubboService
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public BaseMapper<Order> getDao() {
        return orderMapper;
    }

    @Override
    public P<Order> queryList(String query, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orders = orderMapper.queryList(query);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        return new P()
                .setPageNum(orderPageInfo.getPageNum())
                .setPageSize(orderPageInfo.getPageSize())
                .setRows(orderPageInfo.getList())
                .setTotal(orderPageInfo.getTotal())
                .setPages(orderPageInfo.getPages());
    }

    @Override
    public List<Map<String, String>> getSetMealReportForLastMonth() {
        return orderMapper.selectSetMealReportForLastMonth();
    }

    @Override
    @Transactional
    public void reservation(Order order, Integer doctor) {
        orderMapper.insert(order);
        int i = orderSettingMapper.updateByUserIdAndDate(doctor,order.getOrderDate());
        if (i==0){
            throw new IllegalArgumentException("预约人数已满!");
        }
    }

    @Override
    @Transactional
    public void insertAll(Order order,Integer doctor) {
        orderMapper.insert(order);
        int i = orderSettingMapper.updateByUserIdAndDate(doctor,order.getOrderDate());
        if (i==0){
            throw new IllegalArgumentException("预约人数已满!");
        }
    }

    @Override
    public void update(Order order) {
        orderMapper.update(order);
    }


}
